<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            background-color: #b2d8f8;
        }
        .container {
            background-color: white;
            width: 350px;
            margin: 0 auto;
            padding: 15px 22px;
            border: 1px solid #ccc;
            border-radius: 15px;
            margin-top: 50vh;
            transform: translate(0, -50%);
        }

        .form-group {
            margin-bottom: 10px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
        }

        .form-group input[type="text"],
        .form-group input[type="password"] {
            width: 90%;
            padding: 15px 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .form-group input[type="submit"] {
            width: 100%;
            font-family: "Arial Black";
            font-size: 17px;
            font-weight: 800;
            background-color: #4490f7;
            color: white;
            border: none;
            margin-top: 15px;
            margin-bottom: 5px;
            height: 45px;
            border-radius: 9px;
            cursor: pointer;
            box-shadow: 0 6px 16px 0 rgb(78 111 242 / 30%);
        }
        .form-group input[type="submit"]:hover {
            background-color: #4662d9;
        }
        a {
            font-size: 14px;
            text-decoration:none;
            color: #38a3ff;

        }
        a:hover, a:visited, a:link, a:active {
            color: #38a3ff;
        }


    </style>
</head>

<body>
<div class="container">
    <h2>登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label>用户名：</label>
            <input type="text" name="username" id="usernameInput" placeholder="请输入用户名">
        </div>
        <div class="form-group">
            <label>密码：</label>
            <input type="password" name="password" id="passwordInput" placeholder="请输入密码">
        </div>
        <div class="form-group">
            <input id="submit" type="submit" value="登录">
        </div>
        <div style="text-align: right;">
            <a href="register.html">
                立即注册
            </a>
        </div>

    </form>
</div>
</body>

</html>
<script>
    document.getElementById('loginForm').addEventListener('submit', function(event) {
        event.preventDefault();

        // 获取用户名和密码输入框的值
        const username = document.getElementById('usernameInput').value;
        const password = document.getElementById('passwordInput').value;

        // 构建登录数据对象
        const loginData = {
            username: username,
            password: password
        };

        // 发起登录请求
        // fetch('http://hanfeiyu.free.idcfengye.com/user/login', {
        fetch('http://localhost/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(loginData)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('网络响应不正常');
                }
                return response.json();
            })
            .then(data => {
                if (data.flag) {
                    alert('登录成功');
                    window.location.href = 'main.html';
                    localStorage.setItem('token', data.data.token);
                    localStorage.setItem('username',data.data.username);
                }else {
                    alert('登录失败，' + data.message)
                    console.error('登录失败:', data.message);
                    // 保存 Token 到本地存储

                    // 处理登录失败的情况，例如显示错误信息给用户
                }
                // 处理登录成功的情况，例如保存 Token 等操作
            })
            .catch(error => {
                console.error('登录请求出现问题:', error);
                // 处理登录失败的情况，例如显示错误信息给用户
            });
    });
</script>
